import json


def python_to_json():
    # 将python对象转成json
    d = {
        'name': 'python书籍',
        'price': 62.3,
        'is_valid': True
    }

    result = json.dumps(d, indent=4)
    print(result)


def json_to_python():
    # 将json转换成python
    data = '''
          {
              "name": "Python书籍",
              "origin_price": 66,
              "pub_date": "2018-4-14 17:00:00",
              "store": ["京东", "淘宝"],
              "author": ["张三", "李四", "Jhone"],
              "is_valid": true,
              "is_sale": false,
              "meta": {
                  "isbn": "abc-123",
                  "pages": 300
              },
              "desc": null
          }
      '''
    result = json.loads(data)
    print(result)
    print(result['name'])


def json_to_python_from_file():
    # 从文件读取内容,并转成python对象
    f = open('./static/book.json', 'r', encoding='utf-8')
    s = f.read()
    print(s)
    result = json.loads(s)
    print(result['name'])

    f.close()


if __name__ == '__main__':
    # python_to_json()
    # json_to_python()
    json_to_python_from_file()
